# -*- coding: utf-8 -*-,
# @File : SQLALchemy.py,
# @Author : lidong,
# @IDEA: PyCharm
# @Time :  2022/4/26 15:05
# 导入:
from sqlalchemy import Column, String, create_engine, Integer
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类:
Base = declarative_base()

# 定义User对象:
class User(Base):
    # 表的名字:
    __tablename__ = 'user'

    # 表的结构:
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255))

# 初始化数据库连接:
engine = create_engine('mysql+pymysql://root:123456@124.220.0.182:3307/17wlw')
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)

session = DBSession()

new_user = User(name='张三')
session.add(new_user)
session.commit()

user = session.query(User).filter(User.id==7).one()
print('type:', type(user))
print('name:', user.name)
session.close()